//weixin-js-sdk应用
import wx from 'weixin-js-sdk'

import {
  getWechatShareOpt,
} from '@/api';
async function wechatShare(info, callback) {
  const { code, data: wx_config } = await getWechatShareOpt({ url: window.location.href })
  if (code === 1000) {
      let config = { 'debug': false , ...wx_config,  jsApiList: [ 'updateAppMessageShareData', 'updateTimelineShareData'] };
      wx.config(config);
      wx.checkJsApi({
        jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
        success: function (res) {
          // 以键值对的形式返回，可用的api值true，不可用为false
          // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
        }
      });
      wx.ready(() => {
        let share_title = info.title;
        let share_desc = info.desc;
        let share_link  = wx_config.url
        // if (!share_link) {
        //   share_link = window.location.href;
        // }
        let share_img = info.img;
        wx.updateAppMessageShareData({
          title: share_title, // 分享标题
          desc: share_desc, // 分享描述
          link: share_link || window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: share_img, // 分享图标
          success: info.success
        });
        wx.updateTimelineShareData({
          title: share_title, // 分享标题
          link: share_link || window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: share_img, // 分享图标
          success: info.success
        });
        wx.error(function (res) {
          console.log(res, '微信jssdk验证');
        });
      });
    }
}
export default wechatShare
